package com.example.finger.fragment;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.example.finger.FingerHelper;
import com.example.finger.bean.TaskSheet;
import com.example.finger.dialog.OpenLockDialog;
import com.example.finger.R;
import com.example.finger.adpter.TaskAdapter;
import com.google.gson.Gson;
import com.lyxmaster.lib.toasty.Toasty;
import com.lyxmaster.lib.util.DataConfig;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by LYX on 2018/11/23.
 */

public class TaskFragment extends MyBaseFragment {
    private ListView listView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_task, container, false);

        listView = rootView.findViewById(R.id.listview);

        listView.setAdapter(new TaskAdapter(FingerHelper.getInStance().taskSheetList,getContext()));
        listView.setOnItemClickListener(onItemClickListener);

        return  rootView;
    }

    private AdapterView.OnItemClickListener onItemClickListener = new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            TaskSheet taskSheet = FingerHelper.getInStance().taskSheetList.get(i);

            long dateNow = new Date().getTime();
            long dataStart = DataConfig.strToDateLong(taskSheet.getTask().getTimestart()).getTime();
            long dataEnd = DataConfig.strToDateLong(taskSheet.getTask().getTimeend()).getTime();

            if(dateNow >= dataStart && dateNow <= dataEnd && taskSheet.getTask().getTimeStatust() != -100) {
                if(!getTimeCompareSize(taskSheet.getTask().getXunjianstart(), taskSheet.getTask().getXunjianend())){
                    Toasty.error(getContext(), "任务单不在巡检时间内").show();
                    return;
                }
                OpenLockDialog openLockDialog = OpenLockDialog.getInstance(new Gson().toJson(FingerHelper.getInStance().taskSheetList.get(i)));
                openLockDialog.show(((FragmentActivity)(getContext())).getSupportFragmentManager(), "open");
            }else {
                Toasty.error(getContext(), "任务单不在有效时间内").show();
            }
        }
    };

    public boolean getTimeCompareSize(String startTime, String endTime){
        int i=0;
        SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");//年-月-日 时-分
        try {
            Date dateStart = dateFormat.parse(startTime);//开始时间
            Date dateEnd = dateFormat.parse(endTime);//结束时间
            Date dateNow = dateFormat.parse(dateFormat.format(new Date(System.currentTimeMillis())));
            // 1 结束时间小于开始时间 2 开始时间与结束时间相同 3 结束时间大于开始时间
            if (dateNow.getTime()<dateStart.getTime()){
                return false;
            }
            if (dateNow.getTime()>dateEnd.getTime()){
                return false;
            }
        } catch (ParseException e) {
            e.printStackTrace();
            return false;
        }
        return  true;
    }
}
